package com.racedepartment.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.racedepartment.model.Model;

public abstract class Dao {

	private Connection connection;
	private final String table_name;
	private final String selectAll = "select * from ";
	private final String select;
	private final String delete;
	private final String insert;
	private final String update;
	private Statement statement;

	public Dao(Connection connection, String table_name, String pk)
			throws SQLException {
		this.connection = connection;
		this.table_name = table_name;
		delete = "DELETE FROM " + table_name + " WHERE " + pk + " = ";
		select = "SELECT * FROM " + table_name + " WHERE " + pk + " = ";
		insert = "INSERT INTO " + table_name;
		update = "UPDATE " + table_name + " SET ";
		statement = connection.createStatement();
	}

	protected ResultSet execute(String sql) throws SQLException {
		ResultSet rs = statement.executeQuery(sql);
		return rs;
	}

	protected void commit() throws SQLException {
		connection.commit();
	}

	protected void rollback() throws SQLException {
		connection.rollback();
	}

	protected ResultSet getAllRecords() throws SQLException {
		ResultSet rs = statement.executeQuery(selectAll + table_name);
		return rs;
	}

	protected ResultSet getRecord(int pkValue) throws SQLException {
		ResultSet rs = statement.executeQuery(select + pkValue);
		return rs;
	}

	protected void deleteRecord(int pkValue) throws SQLException {
		statement.executeQuery(delete + pkValue);
	}

	protected ResultSet insertRecord(Model model) throws SQLException {
		ResultSet rs = null;
		String[] fields = model.getFields();
		int[] dataTypes = model.getDataTypes();
		String[] values = model.getValues();
		StringBuilder sql = new StringBuilder(insert);
		sql.append("(");
		if (fields.length == values.length && values.length == dataTypes.length) {
			for (int i = 0; i < fields.length; i++) {
				if (!values[i].equals("-1")) {
					sql.append(fields[i]);
					if (i < fields.length - 1) {
						sql.append(" , ");
					} else {
						sql.append(") VALUES (");
					}
				}
			}
			for (int i = 0; i < values.length; i++) {
				if (!values[i].equals("-1")) {
					if (dataTypes[i] == Model.DATATYPE_TEXT) {
						sql.append("'");
						sql.append(values[i]);
						sql.append("'");
					} else if (dataTypes[i] == Model.DATATYPE_INT
							|| dataTypes[i] == Model.DATATYPE_BOOLEAN) {
						sql.append(values[i]);
					}
					if (i < values.length - 1) {
						sql.append(" , ");
					} else {
						sql.append(")");
					}
				}
			}
			statement.executeUpdate(sql.toString());
			rs = statement.getGeneratedKeys();
		}
		return rs;	
	}

	protected void updateRecord(Model model) throws SQLException {
		String[] fields = model.getFields();
		String[] values = model.getValues();
		int[] dataTypes = model.getDataTypes();
		StringBuilder sql = new StringBuilder(update);
		if (fields.length == values.length) {
			for (int i = 0; i < fields.length; i++) {
				sql.append(fields[i]);
				sql.append(" = ");
				if (dataTypes[1] == Model.DATATYPE_TEXT) {
					sql.append("'");
					sql.append(values[i]);
					sql.append("'");
				} else if (dataTypes[i] == Model.DATATYPE_INT
						|| dataTypes[i] == Model.DATATYPE_BOOLEAN) {
					sql.append(values[i]);
				}
				if (i < fields.length - 1) {
					sql.append(" , ");
				}
			}
			statement.execute(sql.toString());
		}
	}

}
